package tacos.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import org.springframework.hateoas.PagedModel;
import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.server.EntityLinks;
import org.springframework.hateoas.server.RepresentationModelProcessor;
import tacos.domain.Taco;


@Configuration
public class SpringDataRestConfiguration {
  /**
   * 这个配置类
   * 如果有控制器返回的是 CollectionModel<TacoResource> ,就会自动多加上 recent的链接
   *     "_links": {
   *         "self": {
   *             "href": "http://localhost:8080/api/tacos"
   *         },
   *         "profile": {
   *             "href": "http://localhost:8080/api/profile/tacos"
   *         },
   *         "recents": {
   *             "href": "http://localhost:8080/api/tacos/recent"
   *         }
   *     }
   * @param links
   * @return
   */
  @Bean
  public RepresentationModelProcessor<PagedModel<EntityModel<Taco>>>
    tacoProcessor(EntityLinks links) {

    return new RepresentationModelProcessor<PagedModel<EntityModel<Taco>>>() {
      @Override
      public PagedModel<EntityModel<Taco>> process(
              PagedModel<EntityModel<Taco>> resource) {
        resource.add(
            links.linkFor(Taco.class)
                 .slash("recent")
                 .withRel("recents"));
        return resource;
      }
    };
  }
  
}
